!############################# Change Log ##################################
! 5.0.0
!
!###########################################################################
!  Copyright (C)  1990, 1995, 1999, 2000, 2003 - All Rights Reserved
!  Regional Atmospheric Modeling System - RAMS
!###########################################################################

subroutine master_getcflcpu()

  use mem_grid, only: &
       cflxy,         & ! INTENT(INOUT)
       cflz,          & ! INTENT(INOUT)
       ngrids           ! INTENT(IN)

  use rpara, only:    &
       nmachs,        & ! INTENT(IN)
       ptimes           ! INTENT(IN)

  use grid_dims, only: &
       maxgrds           ! INTENT(IN)

  implicit none

  integer            :: ngr,nm, ihostnum
  real, allocatable  :: buff(:), buff1(:), buff2(:)
  integer            :: nwords, max_size, ipos
  integer, parameter :: tag=999

  nwords = 2 + 2*maxgrds
  call parf_pack_max_size(nwords, max_size)
  allocate (buff(max_size))
  allocate (buff1(maxgrds))
  allocate (buff2(maxgrds))

  cflxy(1:ngrids) = 0.
  cflz (1:ngrids) = 0.

  do nm=1,nmachs
     call parf_get_block_any(buff, max_size, tag, ihostnum)
     ipos = 1
     call parf_unpack_real(buff, max_size, ipos, ptimes(ihostnum,1), 1)
     call parf_unpack_real(buff, max_size, ipos, ptimes(ihostnum,2), 1)
     call parf_unpack_real(buff, max_size, ipos, buff1, maxgrds)
     call parf_unpack_real(buff, max_size, ipos, buff2, maxgrds)     
     do ngr = 1,ngrids
        cflxy(ngr) = max(cflxy(ngr), buff1(ngr))
        cflz(ngr)  = max(cflz(ngr),  buff2(ngr))
     enddo
  enddo

  deallocate(buff, buff1, buff2)

end subroutine master_getcflcpu

!     *****************************************************************

subroutine node_putcflcpu(totcpu, totwall)

  use mem_grid, only: &
       cflxy,         & ! INTENT(IN)
       cflz             ! INTENT(IN)

  use node_mod, only: &
       master_num       ! INTENT(IN)

  use grid_dims, only: &
       maxgrds           ! INTENT(IN)

  implicit none
  ! Arguments:
  real, intent(in)   :: totcpu, totwall
  ! Local Variables:
  real, allocatable  :: buff(:)
  integer            :: nwords, max_size, ipos
  integer, parameter :: tag=999

  nwords = 2 + 2 * maxgrds
  call parf_pack_max_size(nwords, max_size)
  allocate (buff(max_size))

  ipos = 1
  call parf_pack_real(totcpu,  1,       buff, max_size, ipos)
  call parf_pack_real(totwall, 1,       buff, max_size, ipos)
  call parf_pack_real(cflxy,   maxgrds, buff, max_size, ipos)
  call parf_pack_real(cflz,    maxgrds, buff, max_size, ipos)
  call parf_send_block(buff, max_size, master_num, tag)

  deallocate(buff)

end subroutine node_putcflcpu

!     *****************************************************************

subroutine master_putdtsched(isendflg,isendlite,isendmean  &
     ,isendboth,ntsend)

  use mem_grid, only: &
       maxsched,      & ! INTENT(IN)
       maxschent,     & ! INTENT(IN)
       nnacoust,      & ! INTENT(IN)
       nndtrat,       & ! INTENT(IN)
       isched,        & ! INTENT(IN)
       nsubs,         & ! INTENT(IN)
       dtlongn,       & ! INTENT(IN)
       sspct            ! INTENT(IN)

  use rpara, only: &
       nmachs,        & ! INTENT(IN)
       machnum,       & ! INTENT(IN)
       maxgrds          ! INTENT(IN)

  implicit none
  ! Arguments:
  integer, intent(in) :: isendflg, isendlite, isendmean, isendboth, ntsend
  ! Local Variables:
  real, allocatable   :: buff(:)
  integer             :: nwords, max_size, ipos
  integer             :: nm
  integer, parameter  :: tag = 44
  
  nwords = 7 + 3 * maxgrds + maxsched * maxschent
  call parf_pack_max_size(nwords, max_size)
  allocate (buff(max_size))

  ipos = 1
  call parf_pack_int(isendflg,  1, buff, max_size, ipos)
  call parf_pack_int(isendlite, 1, buff, max_size, ipos)
  call parf_pack_int(isendmean, 1, buff, max_size, ipos)
  call parf_pack_int(isendboth, 1, buff, max_size, ipos)
  call parf_pack_int(ntsend,    1, buff, max_size, ipos)
  if(ntsend == 1) then
     call parf_pack_int (nnacoust, maxgrds,            buff, max_size, ipos)
     call parf_pack_int (nndtrat,  maxgrds,            buff, max_size, ipos)
     call parf_pack_int (isched,   maxsched*maxschent, buff, max_size, ipos)
     call parf_pack_int (nsubs,    1,                  buff, max_size, ipos)
     call parf_pack_real(dtlongn,  maxgrds,            buff, max_size, ipos)
     call parf_pack_real(sspct,    1,                  buff, max_size, ipos)
  endif

  do nm=1,nmachs
     call parf_send_block(buff, max_size, machnum(nm), tag)
  enddo

  deallocate(buff)

end subroutine master_putdtsched

!     *****************************************************************

subroutine node_getdtsched(isendflg,isendlite,isendmean,isendboth)

  use mem_grid, only: &
       maxsched,      & ! INTENT(IN)
       maxschent,     & ! INTENT(IN)
       nnacoust,      & ! INTENT(IN)
       nndtrat,       & ! INTENT(IN)
       isched,        & ! INTENT(IN)
       nsubs,         & ! INTENT(IN)
       dtlongn,       & ! INTENT(IN)
       sspct            ! INTENT(IN)

  use rpara, only: &
       maxgrds       ! INTENT(IN)

  use node_mod, only: &
       master_num    ! INTENT(IN)
  
  implicit none
  integer :: isendflg,isendlite,isendmean,isendboth,ntsend

  real, allocatable :: buff(:)
  integer :: nwords, max_size, ipos
  integer :: ihostnum
  integer, parameter :: tag=44

  nwords = 7 + 3 * maxgrds + maxsched * maxschent
  call parf_pack_max_size(nwords, max_size)
  allocate (buff(max_size))

  call parf_get_block(buff, max_size, master_num, tag)

  ipos = 1
  call parf_unpack_int(buff, max_size, ipos, isendflg,  1)
  call parf_unpack_int(buff, max_size, ipos, isendlite, 1)
  call parf_unpack_int(buff, max_size, ipos, isendmean, 1)
  call parf_unpack_int(buff, max_size, ipos, isendboth, 1)
  call parf_unpack_int(buff, max_size, ipos, ntsend,    1)

  if(ntsend == 1) then
     call parf_unpack_int (buff, max_size, ipos, nnacoust, maxgrds)
     call parf_unpack_int (buff, max_size, ipos, nndtrat,  maxgrds)
     call parf_unpack_int (buff, max_size, ipos, isched,   maxsched*maxschent)
     call parf_unpack_int (buff, max_size, ipos, nsubs,    1)
     call parf_unpack_real(buff, max_size, ipos, dtlongn,  maxgrds)
     call parf_unpack_real(buff, max_size, ipos, sspct,    1)
  endif

  deallocate(buff)
  
end subroutine node_getdtsched




